package cn.com.homeyoda.aimate.aihealth.ui.health;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.FamilyMembersBean;
import cn.com.homeyoda.aimate.aihealth.bean.RecommendRecipeBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityDietPlanBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.DietPlanAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.DietPlanViewMode;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyActivity;
import cn.com.homeyoda.aimate.aihealth.views.MemberChangeDialog;

/**
 * 饮食计划
 */
public class DietPlanActivity extends BaseActivity<ActivityDietPlanBinding, DietPlanViewMode> {

    private DietPlanAdapter breakfastAdapter, lunchAdapter, dinnerAdapter;
    MemberChangeDialog memberChangeDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public DietPlanViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(DietPlanViewMode.class);
    }

    @Override
    public void initData() {
        super.initData();

        initView();
        viewModel.initUserInfo();
    }

    private void initView() {


        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.tvTitle.setText(R.string.diet_plan);

        initRecyclerView();

        binding.btnChangeMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewModel.planUserStateLiveData.getValue() == null) {
                    ToastUtils.showLong("成员未初始化，请稍后重试");
                    return;
                }

                if (memberChangeDialog == null) {
                    memberChangeDialog = new MemberChangeDialog(DietPlanActivity.this, viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId()
                            , new MemberChangeDialog.SelectClickListener() {
                        @Override
                        public void onSelectMember(FamilyMembersBean.DataBean.FamilyMembers member) {
                            viewModel.changeMember(member);
                        }
                    });
                } else {
                    memberChangeDialog.setDatas(viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId());
                }

                memberChangeDialog.show();
            }
        });

        binding.btnAddMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyActivity.class);
                finish();
            }
        });
    }

    private void initRecyclerView() {

        breakfastAdapter = new DietPlanAdapter(R.layout.item_diet_plan, new ArrayList<>(), this);
        lunchAdapter = new DietPlanAdapter(R.layout.item_diet_plan, new ArrayList<>(), this);
        dinnerAdapter = new DietPlanAdapter(R.layout.item_diet_plan, new ArrayList<>(), this);

        binding.rvBreakfast.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.rvLunch.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.rvDinner.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        binding.rvBreakfast.setAdapter(breakfastAdapter);
        binding.rvLunch.setAdapter(lunchAdapter);
        binding.rvDinner.setAdapter(dinnerAdapter);


    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.planUserStateLiveData.observe(this, userBean -> {
            binding.setPlanUserBean(userBean);
            GlideLoadUtils.loadImg(binding.crvHead, userBean.getAvatar(), R.mipmap.ic_member1);
            viewModel.getFoodData();
        });

        viewModel.planUserStateLiveData.stateEnumMutableLiveData.observe(this, state -> {
            Log.d("pangtao", "stateEnumMutableLiveData state = " + state);
            if (state == StateLiveData.StateEnum.Error) {
                binding.clNotUser.setVisibility(View.VISIBLE);
                binding.clUserInfo.setVisibility(View.INVISIBLE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.clContent.setVisibility(View.INVISIBLE);
            } else if (state == StateLiveData.StateEnum.Success) {
                binding.clContent.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                binding.clNotUser.setVisibility(View.INVISIBLE);
                binding.clUserInfo.setVisibility(View.VISIBLE);
            }
        });

        viewModel.dmiStateLiveData.observe(this, bmi -> {
            if (bmi > 40) {
                bmi = 40D;
            }
            binding.tvDmi.setText(bmi.toString());
            if (bmi < 18.5) {
                binding.tvBmiState.setText(R.string.thin);
                binding.tvBmiState.setTextColor(getResources().getColor(R.color.clr_thin));
                binding.tvDmi.setTextColor(getResources().getColor(R.color.clr_thin));
                binding.tvBmiState.setBackgroundResource(R.drawable.bg_rectangle_thin);

                double progress = 16.7 / 18.5 * bmi;
                binding.seekbar.setProgress((int) Math.round(progress));
            } else if (bmi >= 18.5 && bmi <= 23.9) {
                binding.tvBmiState.setText(R.string.right);
                binding.tvBmiState.setTextColor(getResources().getColor(R.color.clr_right));
                binding.tvDmi.setTextColor(getResources().getColor(R.color.clr_right));
                binding.tvBmiState.setBackgroundResource(R.drawable.bg_rectangle_right);

                double progress = 33.33 / (23.9 - 18.5) * (bmi - 18.5) + 18.5;
                binding.seekbar.setProgress((int) Math.round(progress));
            } else if (bmi >= 24 && bmi <= 27.9) {
                binding.tvBmiState.setText(R.string.slightly);
                binding.tvBmiState.setTextColor(getResources().getColor(R.color.clr_slightly));
                binding.tvDmi.setTextColor(getResources().getColor(R.color.clr_slightly));
                binding.tvBmiState.setBackgroundResource(R.drawable.bg_rectangle_slightly);

                double progress = 33.33 / (27.9 - 24) * (bmi - 23.9) + 18.5 + 33.33;
                binding.seekbar.setProgress((int) Math.round(progress));
            } else if (bmi >= 28) {
                binding.tvBmiState.setText(R.string.fat);
                binding.tvBmiState.setTextColor(getResources().getColor(R.color.clr_fat));
                binding.tvDmi.setTextColor(getResources().getColor(R.color.clr_fat));
                binding.tvBmiState.setBackgroundResource(R.drawable.bg_rectangle_fat);

                double progress = 16.7 / (40 - 28) * (bmi - 27.9) + 18.5 + 33.33 + 33.33;
                binding.seekbar.setProgress((int) Math.round(progress));
            }


        });

        viewModel.foodStateLiveData.observe(this, foods -> {
            for (RecommendRecipeBean.DataBean.MenuFoodListBean menuFoodListBean : foods) {
                if (menuFoodListBean.getMenuId().equals("1")) {
                    breakfastAdapter.setNewData(menuFoodListBean.getFoodList());
                    binding.tvHeatBreakfast.setText("建议" + menuFoodListBean.getSuggestCalorie() + "千卡");
                } else if (menuFoodListBean.getMenuId().equals("2")) {
                    lunchAdapter.setNewData(menuFoodListBean.getFoodList());
                    binding.tvHeatLunch.setText("建议" + menuFoodListBean.getSuggestCalorie() + "千卡");
                } else if (menuFoodListBean.getMenuId().equals("3")) {
                    dinnerAdapter.setNewData(menuFoodListBean.getFoodList());
                    binding.tvHeatDinner.setText("建议" + menuFoodListBean.getSuggestCalorie() + "千卡");
                }
            }
        });
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_diet_plan;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}